<%@page import="java.util.LinkedList"%>
<%@page import="java.util.List"%>
<%@page import="java.text.DateFormat"%>
<%@page import="lbsproject.processaudit.model.CheckTask"%>
<%@page import="lbsproject.processaudit.model.CheckPoint"%>
<%@page import="lbsproject.processaudit.model.WorkRegion"%>
<%@page import="lbsproject.processaudit.model.enumeration.TaskLevel"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>巡检项目任务管理</title>
<link href="css/global.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src="js/jquery1.4.4.js"></script>
<script type="text/javascript" src="js/global.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
<script language="javascript" type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script>
</head>
<body>
	<jsp:include page="WEB-INF/template/header.jsp">
		<jsp:param value="current" name="taskManage"/>
	</jsp:include>
	<div id="container" class="section">
		<div class="main">
			<jsp:include page="WEB-INF/template/mapQuery.jsp"/>
			<div style="width:650px;height:420px;border:1px solid gray" id="baidumap"></div>
		</div>
		<div class="aside">
			<ul class="side-nav">
				<li class="nav-item high-light"><a id="side-nav-1" class="nav-base" href="taskManage.jsp"><b></b><em>实时任务情况</em></a></li>
				<li class="nav-item"><a id="side-nav-2" class="nav-base" href="ShowCheckTaskAssigns"><b></b><em>新增任务</em></a></li>
				<li class=""><a id="side-nav-3" class="nav-base" href="ListWorkRegions"><b></b><em>任务片区</em><s></s></a>
				</li>
			</ul>
		</div>
		<div class="clear"></div>
	</div>
	<jsp:include page="WEB-INF/template/footer.jsp"></jsp:include>
</body>
<script type="text/javascript">

var baidu = new BMap.Map("baidumap");                        // 创建Map实例
var workers = new Object();
var regions = new Object();
var taskType = new Object();
var markers = new Array();
var assignDate    = new Object();
var poiToTask = new Object();
var tasks ;
var globalDelay = 10000;
var title = "巡检系统    实时任务    更新时间" ;

var unexecIcon = new BMap.Icon( 
    "http://api.map.baidu.com/img/markers.png",new BMap.Size(28, 37),
    {
	offset: new BMap.Size(10, 25),
	imageOffset: new BMap.Size(0, 0 - 11 * 25)
    }
);

var execIcon = new BMap.Icon( 
    "http://api.map.baidu.com/img/markers.png",new BMap.Size(28, 37),
    {
	offset: new BMap.Size(10, 25),
	imageOffset: new BMap.Size(0, 0 - 12 * 25)
    }
);

var finishIcon = new BMap.Icon( 
    "http://api.map.baidu.com/img/markers.png",new BMap.Size(28, 37),
    {
	offset: new BMap.Size(10, 25),
	imageOffset: new BMap.Size(0, 0 - 10 * 25)
    }
);

var unknownIcon = new BMap.Icon( 
    "http://api.map.baidu.com/img/markers.png",new BMap.Size(28, 37),
    {
	offset: new BMap.Size(10, 25),
	imageOffset: new BMap.Size(0, 0 - 1 * 25)
    }
);

function add_elements_in_a_group(group, obj){
    var i = 1 ;
    var sel = document.getElementById(group);
    var cur = sel.options[sel.selectedIndex].text ;
    sel.options.length = 1;  
    for( name in obj){
	//alert(name)
	var opt = document.createElement('OPTION');
	try
	{
	    sel.add(opt,null); // standards compliant
	}
	catch(ex)
	{
	    sel.add(opt); // IE only
	}

	opt.key = name ;
	opt.text = name ;
	if( name == cur)
	    sel.selectedIndex = i ;
	i++ ;
    }
}

function add_element_to_its_group(obj, name, index){
    if(obj[name] == null)
	obj[name] = 1;
}

function init_baidu_map(){
    baidu.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
    baidu.addControl(new BMap.NavigationControl());
    baidu.addControl(new BMap.ScaleControl());
    baidu.addControl(new BMap.OverviewMapControl());
}

function click_marker(){
    var p = this.getPosition();
    var t = tasks[poiToTask[p.lng +":" + p.lat]];
    var sContent = "<li>创建时间    " + t.CDate + "</li>";
    sContent += "<li>委派者          " + t.AStaff + "</li>";
    sContent += "<li>执行者          " + t.Worker + "</li>";
    sContent += "<li>地点              " + t.Point + "</li>";
    sContent += "<li>任务状态      " + t.StatusDesc + "</li>";
    sContent += "<li>任务类型      " + t.Type + "</li>";
    sContent += "<li>任务级别      " + t.LevelDesc + "</li>";
    sContent += "<li>执行结果      " + t.ResultDesc + "</li>";
    sContent += "<li>所属片区      " + t.Region + "</li>";
    sContent += "<li>委派时间      " + t.ADate + "</li>";
    sContent += "<li>完成时间      " + t.FDate + "</li>";
    
    var opts = {
	title : "任务详细信息"  // 信息窗口标题
    };
    
    var infoWindow = new BMap.InfoWindow(sContent, opts); 
    this.openInfoWindow(infoWindow);
}

function free_all_markers(){
    if(typeof(markers) == "undefined")
	return ;
    
    var i = 0 ;	
    for( ;  i < markers.length ; ++i)
	baidu.removeOverlay(markers[i]) ;

    markers = null ;
    markers = new Array() ;
}

function free_global_object(obj){
    obj = null ;
    obj = new Object() ;	
}

function free_all_global_objects(){
    free_global_object(workers);
    free_global_object(regions);
    free_global_object(taskType);
    free_global_object(assignDate);
    free_global_object(poiToTask);						
}

$(document).ready(function()
		  {
		      init_baidu_map();
		      load_data();
		  }
);

function is_group_let_show(n, group){
    var sel = document.getElementById(group) ;
    var t = sel.options[sel.selectedIndex].text ;

    if ( t == "全部")
	return true ;
    
    if(t == n)
	return true ;
    else
	return false ;
}

function is_date_let_show(n){
    var d = document.getElementById('d523') ;
    var t = d.value;
    
    if ( t == "")
	return true ;
    
    if(t == n)
	return true ;
    else
	return false ;
}

function set_select_disabled(id, t){
    var sel = document.getElementById(id) ;
    sel.disabled = t ;
}

function disable_all_selects(){
    set_select_disabled('member', true);
    set_select_disabled('area', true);
    set_select_disabled('type', true);
}

function enable_all_selects(){
    set_select_disabled('member', false);
    set_select_disabled('area', false);
    set_select_disabled('type', false);
}


function show_tasks(){
    var i = 0 ;
    for( ; i < tasks.length ; ++i){
	var t = tasks[i];
	if(!is_group_let_show(t.Worker, 'member')){
	    markers[i].hide();
	    continue ;
	}
	
	if(!is_group_let_show(t.Type, 'type')){
	    markers[i].hide();
	    continue ;
	}
	
	if(!is_group_let_show(t.Region, 'area')){
	    markers[i].hide();
	    continue ;
	}
	
	if(! is_date_let_show(t.ADate)){
	    markers[i].hide();
	    continue ;
	}

	markers[i].show();
    }
}

function init_data(){
    var i ;
    if(typeof(tasks) == 'undefined')
        return ;

    for(i = 0 ; i < tasks.length; i++ ){
	var t = tasks[i] ;
	var point = new BMap.Point(t.Lng,t.Lat);
	var marker ;
	var poi ;
	
	if(t.Status == 1)
	    marker = new BMap.Marker(point, unexecIcon);
	else if(t.Status == 2)
	    marker = new BMap.Marker(point, execIcon) ;
	else if(t.Status == 3)
	    marker = new BMap.Marker(point, finishIcon) ;
	else 
	    marker = new BMap.Marker(point, unknownIcon) ;

	marker.addEventListener("click", click_marker) ;
	poi = marker.getPosition();

	add_element_to_its_group(workers, t.Worker, i);
	add_element_to_its_group(regions, t.Region, i);
	add_element_to_its_group(taskType, t.Type, i);
	add_element_to_its_group(assignDate, t.ADate, i);

	poiToTask[poi.lng+":"+poi.lat] = i ;
	markers[i] = marker ;
	baidu.addOverlay(marker);

	//marker.setAnimation(BMAP_ANIMATION_BOUNCE); 
    }
}

var str = '[{"Lng":"116.4063","Lat":"39.908802","Type":"特色","Point":"山东分舵","Status":"1","StatusDesc":"未执行","CDate":"2011-08-27","ADate":"2011-08-27","FDate":"2011-8-27","Level":"1","LevelDesc":"NORMAL","Result":"0","ResultDesc":"未知","AStaff":"王乐业","Worker":"邵博","Region":"王府井"},{"Lng":"116.429296","Lat":"39.915","Type":"特色","Point":"阿德","Status":"1","StatusDesc":"未执行","CDate":"2011-08-27","ADate":"2011-08-27","FDate":"2011-8-27","Level":"1","LevelDesc":"NORMAL","Result":"0","ResultDesc":"未知","AStaff":"王乐业","Worker":"子航","Region":"中关村"},{"Lng":"116.399401","Lat":"39.952179","Type":"上帝","Point":"东城","Status":"1","StatusDesc":"未执行","CDate":"2011-08-27","ADate":"2011-08-27","FDate":"2011-8-27","Level":"1","LevelDesc":"NORMAL","Result":"0","ResultDesc":"未知","AStaff":"王乐业","Worker":"老倪","Region":"中关村"},{"Lng":"116.449296","Lat":"39.915","Type":"特色","Point":"阿德","Status":"1","StatusDesc":"未执行","CDate":"2011-08-27","ADate":"2011-08-27","FDate":"2011-8-27","Level":"1","LevelDesc":"NORMAL","Result":"0","ResultDesc":"未知","AStaff":"王乐业","Worker":"Penny","Region":"中关村"},{"Lng":"116.45","Lat":"40.0","Type":"特色","Point":"测试地点","Status":"1","StatusDesc":"未执行","CDate":"2011-08-27","ADate":"2011-08-27","FDate":"2011-8-27","Level":"1","LevelDesc":"NORMAL","Result":"0","ResultDesc":"未知","AStaff":"王乐业","Worker":"Sheldon","Region":"中关村"}]';

function load_data(){
    $.ajax({
	url: "ListAllCheckTasks",
	cache: false,
	dataType: "json",
	success: function(msg){
	    disable_all_selects();
	    free_all_global_objects();
	    tasks = null ;
	    tasks = new Array();
	    free_all_markers() ;
	    //tasks = eval(str);
	    tasks = msg ;
	    init_data();
	    add_elements_in_a_group('member', workers);
	    add_elements_in_a_group('area', regions);
	    add_elements_in_a_group('type', taskType);
	    enable_all_selects();
	    show_tasks();
	}
    }); 
} 

function TimerStart(func,delay)
{
    var TimerID = window.setInterval(
	function(){
	    if(!func()){
		window.clearInterval(TimerID);
	    };
	},
	delay
    );
}

window.onload = function()
{	                        
    document.title = title + new Date();
    
    TimerStart(
        function(){    
	    document.title = title + new Date();
	    load_data();
	    return true;
	},
        globalDelay
    );
}
	
</script>
</html>
